<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>C语言基础 - 运算符</title>
    <link rel="stylesheet" href="common2.css">
    <style>
        /* 页面特有样式 */
        .operator-category {
            background-color: #f8f8f8;
            border-radius: 5px;
            padding: 15px;
            margin-bottom: 20px;
            border-left: 4px solid #5D9CEC;
        }
        
        .operator-table {
            width: 100%;
            margin: 15px 0;
            border-collapse: collapse;
        }
        
        .precedence-list {
            background-color: #fff6e5;
            padding: 15px;
            border-radius: 5px;
        }
        
        .expression-example {
            background-color: #f0f7ff;
            padding: 15px;
            border-radius: 5px;
            margin: 15px 0;
        }
        
        .important-note {
            background-color: #ffecec;
            border-left: 4px solid #ff6b6b;
            padding: 12px;
            margin: 15px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>C语言基础入门课程</h1>
        </header>
        
        <nav>
            <a href="index.html">首页</a>
            <a href="02-intro.html">C语言简介与环境搭建</a>
            <a href="03-structure.html" class="current">C程序结构</a>
            <a href="04-datatypes.html">基本数据类型</a>
            <a href="05-variables.html">变量与常量</a>
            <a href="06-operators.html">运算符</a>
            <a href="07-io.html" class="current">输入输出</a>
            <a href="08-conditionals.html">条件语句</a>
            <a href="09-loops.html">循环结构</a>
            <a href="10-functions.html" class="current">函数基础</a>
            <a href="11-scope.html">作用域与存储类</a>
            <a href="12-recursion.html">递归函数</a>
            <a href="13-arrays.html">一维数组</a>
            <a href="14-multidim.html" class="current">多维数组</a>
            <a href="15-strings.html">字符串处理</a>
            <a href="16-pointers.html">指针基础</a>
            <a href="17-pointers_advanced.html" class="current">指针进阶</a>
            <a href="18-structs.html">结构体</a>
            <a href="19-files.html">文件操作</a>
            <a href="20-project.html">综合项目</a>
        </nav>
        
        <main>
            <h2>C语言运算符</h2>
            
            <section>
                <h3>基本运算符分类</h3>
                
                <div class="operator-category">
                    <h4>1. 算术运算符</h4>
                    <table class="operator-table">
                        <tr>
                            <th>运算符</th>
                            <th>描述</th>
                            <th>示例</th>
                            <th>结果</th>
                        </tr>
                        <tr>
                            <td>+</td>
                            <td>加法</td>
                            <td>5 + 3</td>
                            <td>8</td>
                        </tr>
                        <tr>
                            <td>-</td>
                            <td>减法</td>
                            <td>5 - 3</td>
                            <td>2</td>
                        </tr>
                        <tr>
                            <td>*</td>
                            <td>乘法</td>
                            <td>5 * 3</td>
                            <td>15</td>
                        </tr>
                        <tr>
                            <td>/</td>
                            <td>除法</td>
                            <td>5 / 3</td>
                            <td>1 (整数除法)</td>
                        </tr>
                        <tr>
                            <td>%</td>
                            <td>取模(余数)</td>
                            <td>5 % 3</td>
                            <td>2</td>
                        </tr>
                        <tr>
                            <td>++</td>
                            <td>自增</td>
                            <td>i++ 或 ++i</td>
                            <td>i增加1</td>
                        </tr>
                        <tr>
                            <td>--</td>
                            <td>自减</td>
                            <td>i-- 或 --i</td>
                            <td>i减少1</td>
                        </tr>
                    </table>
                    
                    <div class="important-note">
                        <p><strong>注意：</strong> 自增/自减运算符的前置(++i)和后置(i++)形式有区别：前置先增减后使用，后置先使用后增减。</p>
                    </div>
                </div>
                
                <div class="operator-category">
                    <h4>2. 关系运算符</h4>
                    <table class="operator-table">
                        <tr>
                            <th>运算符</th>
                            <th>描述</th>
                            <th>示例</th>
                            <th>结果(真=1,假=0)</th>
                        </tr>
                        <tr>
                            <td>==</td>
                            <td>等于</td>
                            <td>5 == 3</td>
                            <td>0</td>
                        </tr>
                        <tr>
                            <td>!=</td>
                            <td>不等于</td>
                            <td>5 != 3</td>
                            <td>1</td>
                        </tr>
                        <tr>
                            <td>&gt;</td>
                            <td>大于</td>
                            <td>5 > 3</td>
                            <td>1</td>
                        </tr>
                        <tr>
                            <td>&lt;</td>
                            <td>小于</td>
                            <td>5 < 3</td>
                            <td>0</td>
                        </tr>
                        <tr>
                            <td>&gt;=</td>
                            <td>大于等于</td>
                            <td>5 >= 3</td>
                            <td>1</td>
                        </tr>
                        <tr>
                            <td>&lt;=</td>
                            <td>小于等于</td>
                            <td>5 <= 3</td>
                            <td>0</td>
                        </tr>
                    </table>
                </div>
                
                <div class="operator-category">
                    <h4>3. 逻辑运算符</h4>
                    <table class="operator-table">
                        <tr>
                            <th>运算符</th>
                            <th>描述</th>
                            <th>示例</th>
                            <th>结果</th>
                        </tr>
                        <tr>
                            <td>&amp;&amp;</td>
                            <td>逻辑与</td>
                            <td>(5>3) && (2<1)</td>
                            <td>0</td>
                        </tr>
                        <tr>
                            <td>||</td>
                            <td>逻辑或</td>
                            <td>(5>3) || (2<1)</td>
                            <td>1</td>
                        </tr>
                        <tr>
                            <td>!</td>
                            <td>逻辑非</td>
                            <td>!(5>3)</td>
                            <td>0</td>
                        </tr>
                    </table>
                    
                    <div class="important-note">
                        <p><strong>短路求值：</strong> 逻辑与(&amp;&amp;)和逻辑或(||)有短路特性，当结果已确定时不再计算后续表达式。</p>
                    </div>
                </div>
            </section>
            
            <section>
                <h3>运算符优先级</h3>
                
                <div class="precedence-list">
                    <h4>常见运算符优先级(从高到低)</h4>
                    <ol>
                        <li>() 括号 - 强制优先级</li>
                        <li>++ -- 后缀自增/自减</li>
                        <li>++ -- 前缀自增/自减, + - 一元正负, ! ~ 逻辑非和按位非</li>
                        <li>* / % 乘除取模</li>
                        <li>+ - 加减</li>
                        <li>&lt; &lt;= > >= 关系运算符</li>
                        <li>== != 相等性运算符</li>
                        <li>&amp;&amp; 逻辑与</li>
                        <li>|| 逻辑或</li>
                        <li>= += -= *= /= %= 赋值运算符</li>
                    </ol>
                    
                    <div class="expression-example">
                        <h4>优先级示例</h4>
                        <pre><code>int result = 5 + 3 * 2;    // 结果是11，不是16
int flag = x > 5 && y < 10; // 先比较，再逻辑与</code></pre>
                    </div>
                    
                    <div class="best-practice">
                        <h4>最佳实践</h4>
                        <ul>
                            <li>使用括号明确优先级，避免依赖记忆</li>
                            <li>复杂的表达式拆分为多个简单表达式</li>
                            <li>避免在单个表达式中使用太多运算符</li>
                        </ul>
                    </div>
                </div>
            </section>
            
            <section>
                <h3>表达式示例</h3>
                
                <div class="expression-example">
                    <h4>1. 算术表达式</h4>
                    <pre><code>// 计算圆的面积
float radius = 5.0f;
float area = 3.14159f * radius * radius;

// 整数除法与浮点数除法
int a = 5, b = 2;
float div1 = a / b;        // 结果为2.0
float div2 = (float)a / b; // 结果为2.5</code></pre>
                </div>
                
                <div class="expression-example">
                    <h4>2. 关系与逻辑表达式</h4>
                    <pre><code>// 判断年份是否为闰年
int year = 2023;
int isLeap = (year % 400 == 0) || (year % 100 != 0 && year % 4 == 0);

// 判断成绩等级
int score = 85;
char grade;
if (score >= 90) grade = 'A';
else if (score >= 80) grade = 'B';
else if (score >= 70) grade = 'C';
else grade = 'D';</code></pre>
                </div>
                
                <div class="expression-example">
                    <h4>3. 复合赋值表达式</h4>
                    <pre><code>// 复合赋值运算符
int x = 10;
x += 5;   // 等同于 x = x + 5
x *= 2;   // 等同于 x = x * 2

// 自增运算符的不同形式
int i = 5;
int j = i++; // j=5, i=6
int k = ++i; // k=7, i=7</code></pre>
                    
                    <div class="important-note">
                        <p><strong>注意：</strong> 避免在同一个表达式中对同一变量多次使用自增/自减运算符，行为可能未定义。</p>
                    </div>
                </div>
            </section>
        </main>
        

        
        <!-- 二维码区域 -->

    </div> <!-- 结束.container -->
</body>
</html>